/**
 * 通用js方法封装处理
 * Copyright (c) 2019 aidex
 */

export function replaceAll(text, stringToFind, stringToReplace) {
	if (stringToFind == stringToReplace) return this;
	var temp = text;
	var index = temp.indexOf(stringToFind);
	while (index != -1) {
		temp = temp.replace(stringToFind, stringToReplace);
		index = temp.indexOf(stringToFind);
	}
	return temp;
}
export function debounce(func, delay = 300, immediate = false) {
	let timer = null;
	return function(...args) {
		const context = this;
		const callNow = immediate && !timer;

		clearTimeout(timer);
		timer = setTimeout(() => {
			timer = null;
			if (!immediate) func.apply(context, args);
		}, delay);

		if (callNow) func.apply(context, args);
	};
}